import 'package:flutter/material.dart';
import 'package:sl_flutter/utils/http.dart';
import 'package:sl_flutter/utils/tokenManager.dart';

class Mine extends StatefulWidget {
  Mine({super.key, required this.token});
  String token;

  @override
  State<Mine> createState() => _MineState();
}

class _MineState extends State<Mine> {
  String token = '';
  // 定义常量，方便修改和维护
  static const double avatarRadius = 50;
  static const double containerPadding = 10;
  static const double containerMargin = 10;
  static const double titleFontSize = 16;
  static const double valueFontSize = 20;
  static const double offsetY = -50;

  // 司机编号
  String driverId = '';
  // 司机头像
  String driverAvatar = '';
  // 司机姓名
  String driverName = '';
  // 司机手机号
  String driverPhone = '';

  @override
  void initState() {
    token = widget.token;
    print('我的${token}');
    // 保存token
    TokenManager().saveToken(token);
    getMyInfo();

    super.initState();
  }

  // 获取我的信息
  getMyInfo() async {
    try {
      final res = await https.get('/driver/users');
      print(res);
      // print(TokenManager().getToken());
      setState(() {
        driverId = res['data']['number'];
        driverAvatar = res['data']['avatar'];
        driverName = res['data']['name'];
        driverPhone = res['data']['phone'];
      });
    } catch (e) {
      print('获取我的信息失败: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            // 头部信息容器
            _buildHeaderContainer(context),
            const SizedBox(height: 20),
            // 本月任务容器
            _buildTaskContainer(),
            // 列表选项容器
            _buildListTileContainer(),
          ],
        ),
      ),
    );
  }

  // 构建头部信息容器
  Widget _buildHeaderContainer(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: const AssetImage('assets/images/bg_page_my.jpg'),
          fit: BoxFit.cover,
        ),
      ),
      padding: const EdgeInsets.all(50),
      width: MediaQuery.of(context).size.width,
      child: Column(
        children: [
          const SizedBox(height: 20),
          CircleAvatar(
            radius: avatarRadius,
            foregroundImage: NetworkImage(driverAvatar),
          ),
          const SizedBox(height: 10),
          Text(
            driverName,
            style: TextStyle(
              color: Colors.white,
              fontSize: 20,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 5),
          Text(
            '司机编号：${driverId}',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
          const SizedBox(height: 5),
          Text(
            '手机号：${driverPhone}',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ],
      ),
    );
  }

  // 构建本月任务容器
  Widget _buildTaskContainer() {
    return Transform.translate(
      offset: const Offset(0, offsetY),
      child: Container(
        height: 130,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
        ),
        margin: const EdgeInsets.all(containerMargin),
        padding: const EdgeInsets.all(containerPadding),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const Text(
              '-本月任务-',
              style: TextStyle(
                fontSize: titleFontSize,
              ),
            ),
            const SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _buildTaskItem('任务总量', '18'),
                _buildTaskItem('完成任务量', '16'),
                _buildTaskItem('运输里程(km)', '3987'),
              ],
            ),
          ],
        ),
      ),
    );
  }

  // 构建列表选项容器
  Widget _buildListTileContainer() {
    return Transform.translate(
      offset: const Offset(0, offsetY),
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          color: Colors.white,
        ),
        padding: const EdgeInsets.all(containerPadding),
        margin: const EdgeInsets.all(containerMargin),
        child: Column(
          children: [
            _buildListTile('车辆信息'),
            _buildListTile('任务数据'),
            _buildListTile('系统设置'),
          ],
        ),
      ),
    );
  }

  // 构建单个任务项
  Widget _buildTaskItem(String title, String value) {
    return Column(
      children: [
        Text(
          value,
          style: const TextStyle(
            fontSize: valueFontSize,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 5),
        Text(
          title,
          style: const TextStyle(
            fontSize: titleFontSize,
          ),
        ),
      ],
    );
  }

  // 构建列表项
  Widget _buildListTile(String title) {
    return ListTile(
      title: Text(title),
      trailing: const Icon(Icons.arrow_forward_ios),
      onTap: () {
        if (title == '系统设置') {
          Navigator.pushNamed(context, '/setting');
        }
      },
    );
  }
}
